home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Aminet 8
/
Aminet 8 (1995)(GTI - Schatztruhe)[!][Oct 1995].iso
/
Aminet
/
comm
/
misc
/
LogTime.lha
/
LogTime
/
LogTime.c
< prev
next >
Wrap
C/C++ Source or Header
|
1995-09-02
|
8KB
|
308 lines
static const char *rcsid = "$Id: LogTime.c,v 1.18 1995/09/02 21:45:44 mleining Exp mleining $";
static const char *version = "Version $VER: $Revision: 1.18 $";
/*
* Copyright (c) 1995, Marvin James (Jim) Leininger, Jr
* mleining@metrolink.net
* All Rights Reserved.
*
* this program is a quick hack to time my ppp connections
*
* I'm providing it "as is" because I thought other's might
* find it useful.
*
* To use it:
*
* 0. copy LogTime to someplace in your path, probably
* AmiTCP:bin or c:
*
* 1. when you connect to your on-line service, run LogTime.
* (if you don't want the timer output,
* use "run >nil: <nil: Logtime <nil: >nil:")
* (a good place to do this is in your startppp
* or startnet script).
*
* 2. when you disconnect from your on-line service,
* run logtime with the -s option.
* (a good place to do this is in your stopppp
* or stopnet script).
*
* 3. once a month (usually on your billing cutoff date), run
* LogTime with the -r option, to reset the timer to zero.
* If you run a cron program, you can add lines to your
* crontab to print your current monthly usage (logtime -p)
* and then reset the counter (logtime -r).
*
* 4. Logtime writes it's time to ENV:LogTimeValue and ENVARC:LogTimeValue,
* so you can access the current time from those files,
* or with the environment functions. I know this is a hack,
* but I didn't want to take the time to figure out the right
* way to do this. It also uses ENV:LogTimeCmd to send itself
* commands.
*
*
* Standard Disclaimer
* -------------------
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* I BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <time.h>
#include <assert.h>
/*
* #include <machine/limits.h> defines clock tick as 60 which is wrong!
*/
#define CLK_TCK 50
#include <signal.h>
/*
* Amigados Delay Function
* timeout is in clock ticks (CLK_TCK)
*/
void Delay (long timeout);
#ifndef FALSE
#define FALSE 0
#define TRUE 1
#endif
/*
* * Local Data Declarations
*/
#define readonly "r"
#define writeonly "w"
char *myname;
/*
* hack to use env/envarc to store my timer
*/
FILE *envfd;
char envfile[80] = "ENV:LogTimeValue";
FILE *envarcfd;
char envarcfile[80] = "ENVARC:LogTimeValue";
FILE *cmdfd;
char cmdfile[80] = "ENV:LogTimeCmd";
int scanfresult;
/*
* values to place in ENV:LogTimeCmd
*/
#define LOGTIME_NO_COMMAND 0
#define LOGTIME_STOP 1
#define LOGTIME_NO_STATUS 0
#define LOGTIME_RUNNING 1
void
usage (myname)
char *myname;
{
fprintf (stderr, "%s\n", rcsid);
fprintf (stderr, "%s\n", version);
fprintf (stderr, "\n");
fprintf (stderr, "Usage: %s [-rpsv]\n", myname);
fprintf (stderr, "\n");
fprintf (stderr, " Copyright © 1995, Marvin James Leininger, Jr.\n");
fprintf (stderr, " <mleining@metrolink.net>\n");
fprintf (stderr, " All rights reserved.\n");
fprintf (stderr, "\n");
fprintf (stderr,
" Use ctrl-c, break C <processid>, or %s -s to stop.\n", myname);
fprintf (stderr, " -r = reset timer to zero\n");
fprintf (stderr, " -p = print current time value and exit\n");
fprintf (stderr, " -s = stop a running logtime\n");
fprintf (stderr, " -v = this message\n");
exit (1);
};
int
main (argc, argv)
char **argv;
int argc;
{
register char *p;
static unsigned int cmdvalue;
static time_t *timeval = 0;
static unsigned long timeint = 0;
static unsigned long timerval = 0;
static unsigned long lasttimerval = 0;
static unsigned long starttime = 0;
static int reset_timer = FALSE;
static int stop_command = FALSE;
static int print_only = FALSE;
static unsigned long timer_hours;
static unsigned long timer_minutes;
/*
* clock ticks to wait, CLK_TCK == 1 second
* check time every 10 seconds. Normally I would set
* this longer, but it there was no quick and dirty
* way to kill the program until it comes out of the
* delay. A signal handler might work....
*/
static const long waittime = CLK_TCK * 10L;
/*
* read the program name and arguments
*/
myname = (argc-- < 1) ? "LOGTIME" : *argv++;
/*
* read the command line options
*/
while ((argc > 0) && (**argv == '-')) {
argc--; /*
* one less argument to process
*/
p = *argv++; /*
* process the current argument
*/
while (*++p != '\0') {
switch (*p) {
case 'v': /*
* help
*/
usage (myname);
break;
case 'r':
reset_timer = TRUE;
break;
case 's':
cmdfd = fopen (cmdfile, writeonly);
assert (cmdfd);
fprintf (cmdfd, "%u", LOGTIME_STOP);
fclose (cmdfd);
Delay (waittime);
cmdfd = fopen (cmdfile, writeonly);
assert (cmdfd);
fprintf (cmdfd, "%u", LOGTIME_NO_COMMAND);
fclose (cmdfd);
stop_command = TRUE;
break;
case 'p':
print_only = TRUE;
break;
default: /*
* undefined option
*/
usage (myname);
break;
};
};
};
/*
* attempt to read an existing logtime file
*/
envarcfd = fopen (envarcfile, readonly);
if ((envarcfd == NULL) || reset_timer) {
/*
* if there is no existing file, then create one
*/
printf ("Resetting Timer\n");
fclose (envarcfd);
envarcfd = fopen (envarcfile, writeonly);
assert (envarcfd);
fprintf (envarcfd, "%lu", timerval);
fclose (envarcfd);
envfd = fopen (envfile, writeonly);
assert (envfd);
fprintf (envfd, "%lu", timerval);
fclose (envfd);
} else { /*
* the environment variable file already exists
*/
scanfresult = fscanf (envarcfd, "%lu", &timerval);
if (scanfresult == 1) {
fclose (envarcfd);
envfd = fopen (envfile, writeonly);
fprintf (envfd, "%lu", timerval);
fclose (envfd);
} else {
fprintf (stderr, "Error reading environment variable file\n");
assert (0);
};
};
if (reset_timer || stop_command) {
exit (0);
};
/*
* adjust start time to reflect time saved in file
*/
timeint = time (timeval); /*
* get the time from the system
*/
timer_hours = timerval / 60L;
timer_minutes = timerval % 60L;
if (print_only) {
printf ("Online timer (hours:min): %5.5lu:%2.2lu\n", timer_hours, timer_minutes);
fflush (stdout);
exit (0);
};
/*
* now that we've initialized our timers, start counting
*/
starttime = timeint - (timerval * 60L);
while (TRUE) {
/*
* only write if time has changed
*/
if (timerval != lasttimerval) {
envarcfd = fopen (envarcfile, writeonly);
assert (envarcfd);
fprintf (envarcfd, "%lu", timerval);
fclose (envarcfd);
envfd = fopen (envfile, writeonly);
assert (envfd);
fprintf (envfd, "%lu", timerval);
fclose (envfd);
};
/*
* check for a command
*/
cmdfd = fopen (cmdfile, readonly);
if (cmdfd != NULL) {
scanfresult = fscanf (cmdfd, "%u", &cmdvalue);
fclose (cmdfd);
switch (cmdvalue) {
case LOGTIME_NO_COMMAND:
break;
case LOGTIME_STOP:
printf ("\n");
fflush (stdout);
exit (0);
break;
default:
break;
};
};
Delay (waittime);
timeint = time (timeval); /*
* get the time from the system
*/
lasttimerval = timerval;
timerval = (timeint - starttime) / 60L;
timer_hours = timerval / 60L;
timer_minutes = timerval % 60L;
printf (" Online timer (hours:min): %5.5lu:%2.2lu \r", timer_hours, timer_minutes);
fflush (stdout);
};
};